// import produce from 'immer';
import { getTables, getdb } from "../services/crudService.js";

export default {
  state: {
    db: [],
    table: [],
    tableName: "",
    projectName: "",
    CName: "",
    isExport: true,
    isImport: true,
    isMock: false,
    mockCount: 50,
    isModuleSql: false,
    isWebControl: false,
    method: ["List", "ListByQuery", "Add", "Edit", "Del"],
    curStep: 0,
  },
  subscriptions: {
    setup({ dispatch, history }) {
      // dispatch({ type: "fetch" })
    },
  },
  reducers: {
    setAll(state, { payload: { table, projectName, CName, isExport, isImport, isMock, isModuleSql, isWebControl, method, mockCount } }) {
      state.table = table;
      state.projectName = projectName;
      state.CName = CName;
      state.isExport = isExport;
      state.isImport = isImport;
      state.isMock = isMock;
      state.mockCount = mockCount;
      state.isModuleSql = isModuleSql;
      state.isWebControl = isWebControl;
      state.method = method;
    },
    setdb(state, { db }) {
      state.db = db;
    },
    settable(state, { table }) {
      state.table = table;
    },
    settableName(state, { tableName }) {
      state.tableName = tableName;
    },
    setprojectName(state, { projectName }) {
      state.projectName = projectName;
    },
    setCName(state, { CName }) {
      state.CName = CName;
    },
    setcurStep(state, { curStep }) {
      state.curStep = curStep;
    },
    // add(state, action) {
    //   // return produce(state, draftState => {
    //   //   draftState.a.b.c.count += 1
    //   // })
    //   state.a += 1;
    //   // return {
    //   //   a: state.a + 1,
    //   // }
    //   // state.a.b.c.count += 10
    //   // return {
    //   //   count: state.count + 1
    //   // };
    // }
  },
  effects: {
    *fetchTables({ type, payload }, { put, call, select }) {
      const response = yield call(getTables, payload);
      yield put({ "type": "settable", table: response })
    },
    *fetchDB({ type, payload }, { put, call, select }) {
      const response = yield call(getdb, payload);
      yield put({ "type": "setdb", db: response })
    },
  },
}
